package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crm.pojo.common.ErrorMessage;
import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.Result;
import com.crm.pojo.common.UserContext;
import com.crm.pojo.domain.Activity;
import com.crm.pojo.domain.User;
import com.crm.pojo.dto.CluePoolDto;
import com.crm.pojo.dto.ClueDto;
import com.crm.pojo.dto.ClueFalseDto;
import com.crm.service.ActivityService;
import com.crm.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//线索接口
@RestController
@RequestMapping("/clues")
@Slf4j
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private ActivityService activityService;

    @GetMapping("/clue/list")
    public PageResult list(@RequestParam(required = false) Long id,
                           @RequestParam(required = false) String phone,
                           @RequestParam(required = false) String owner,
                           @RequestParam(required = false) String channel,
                           @RequestParam(required = false) String status,
                           @RequestParam(value = "params[beginCreateTime]", required = false) String beginCreateTime,
                           @RequestParam(value = "params[endCreateTime]", required = false) String endCreateTime) {
        ClueDto clueDto = new ClueDto();
        clueDto.setClueId(id);
        clueDto.setPhone(phone);
        clueDto.setOwner(owner);
        clueDto.setChannel(channel);
        clueDto.setStatus(status);
        clueDto.setBeginCreateTime(beginCreateTime);
        clueDto.setEndCreateTime(endCreateTime);
        return clueService.list(clueDto);
    }

    //线索查找（id）
    @GetMapping("/clue/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.success(clueService.getById(id));
    }


    //线索新增
    @PostMapping("/clue")
    public Result addClue(@RequestBody ClueDto clueDto) {
        try {
            clueService.add(clueDto);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    //线索跟进
    @PostMapping("/record")
    public Result recored(@RequestBody ClueDto clueDto) {
        try {
            clueService.record(clueDto);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    //线索跟进记录查询
    @GetMapping("/record/list")
    public PageResult clueRecord(Long clueId) {
        return clueService.clueRecord(clueId);
    }

    //线索池分页查询
    @GetMapping("/clue/pool")
    public PageResult cluePool(@RequestParam(required = false) Long id,
                               @RequestParam(required = false) String phone,
                               @RequestParam(required = false) String owner,
                               @RequestParam(required = false) String channel,
                               @RequestParam(required = false) String status,
                               @RequestParam(value = "params[beginCreateTime]", required = false) String beginCreateTime,
                               @RequestParam(value = "params[endCreateTime]", required = false) String endCreateTime) {
        ClueDto clueDto = new ClueDto();
        clueDto.setClueId(id);
        clueDto.setPhone(phone);
        clueDto.setOwner(owner);
        clueDto.setChannel(channel);
        clueDto.setStatus(status);
        clueDto.setBeginCreateTime(beginCreateTime);
        clueDto.setEndCreateTime(endCreateTime);
        clueDto.setIsPool(1);
        return clueService.list(clueDto);
    }
    //线索转伪线索
    @PutMapping("/clue/false/{id}")
    public Result clueFalse(@PathVariable Long id, @RequestBody ClueFalseDto clueFalseDto) {
        try {
            clueFalseDto.setClueId( id);
            String result = clueService.clueFalse(clueFalseDto);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }

    }
    //线索分配（批量）
    @PutMapping("/clue/assignment")
    public Result assignment(@RequestBody CluePoolDto cluePoolDto) {
        try {
            return Result.success( clueService.assignment(cluePoolDto));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    //线索池批量捞取
    @PutMapping("/clue/gain")
    public Result gain(@RequestBody CluePoolDto cluePoolDto) {
        try {
            return Result.success(clueService.gain(cluePoolDto));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    //线索转商机
    @PutMapping("/clue/changeBusiness/{id}")
    public Result changeBusiness(@PathVariable Long id) {
        try {
            return Result.success(clueService.changeBusiness(id));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
